package com.dhcc.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class __List {
	public static interface Func<TKey, TValue>{
		TKey invoke(TValue value);
	}
//	public static <TKey, TValue> Map<TKey, List<TValue>> groupByField(List<TValue> list, String valueField){
//		
//	}
	
	public static <TKey, TValue> Map<TKey, List<TValue>> group(List<TValue> list, Func<TKey, TValue> func){
		if(list == null){
			return null;
		}
		if(func == null){
			throw new RuntimeException("func 参数不能为 null");
		}
		Map<TKey, List<TValue>> map = new HashMap<TKey, List<TValue>>();
		for (TValue value : list) {
			TKey key = func.invoke(value);
			List<TValue> temp;
			if((temp = map.get(key))==null){
				temp = new ArrayList<TValue>();
				map.put(key, temp);
			}
			temp.add(value);
		}
		return map;
	}
}
